﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Forms;
using Silvermoon.Animations;

namespace Silvermoon.Controls.Textboxes
{
    public class CutCopyPasteToolTip : ToolTip
    {
        private static CutCopyPasteToolTip instance;

        private BubbleButton cut, copy, paste;

        public static CutCopyPasteToolTip Instance
        {
            get
            {
                if (instance == null) instance = new CutCopyPasteToolTip();
                return instance;
            }
        }


        private CutCopyPasteToolTip()
            : base()
        {
            cut = new BubbleButton("Cut");
            copy = new BubbleButton("Copy");
            paste = new BubbleButton("Paste");

            Items.Add(cut);
            Items.Add(copy);
            Items.Add(paste);
            Width = 320;

            cut.Tap += OnCut;
            copy.Tap += OnCopy;
            paste.Tap += OnPaste;
        }

        public TextboxBase TextBox { get { return AnchorControl as TextboxBase; } }

        private void OnCopy(object sender, EventArgs e)
        {
            if (TextBox != null) TextBox.Copy();
            Hide();
        }

        private void OnCut(object sender, EventArgs e)
        {
            if (TextBox!=null) TextBox.Cut();
            Hide();
        }

        private void OnPaste(object sender, EventArgs e)
        {
            if (TextBox != null) TextBox.Paste();
            Hide();
        }


        protected override void OnHidden()
        {
            AnchorControl = null;
            base.OnHidden();
        }

        public void ShowTip()
        {
            Show();
        }

        protected override void OnAnchorControlChanging(Control newControl)
        {
            base.OnAnchorControlChanging(newControl);
            Items.Clear();
            TextboxBase textBox = newControl as TextboxBase;

            if (textBox != null)
            {
                bool readOnly = textBox.ReadOnly;
                if (!readOnly) Items.Add(cut);
                Items.Add(copy);
                if (!readOnly) Items.Add(paste);
                Width = 100 * Items.Count;

            }
        }
    }
}
